<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/6
 * Time: 14:38
 */
namespace App\Handlers;

trait BackendApiResponse
{
    public $statusTexts = array(
        100 => 'Continue',
        101 => 'Switching Protocols',
        102 => 'Processing',            // RFC2518
        103 => 'Early Hints',
        200 => 'OK',
        201 => 'Created',
        202 => 'Accepted',
        203 => 'Non-Authoritative Information',
        204 => 'No Content',
        205 => 'Reset Content',
        206 => 'Partial Content',
        207 => 'Multi-Status',          // RFC4918
        208 => 'Already Reported',      // RFC5842
        226 => 'IM Used',               // RFC3229
        300 => 'Multiple Choices',
        301 => 'Moved Permanently',
        302 => 'Found',
        303 => 'See Other',
        304 => 'Not Modified',
        305 => 'Use Proxy',
        307 => 'Temporary Redirect',
        308 => 'Permanent Redirect',    // RFC7238
        400 => 'Bad Request',
        401 => 'Unauthorized',
        402 => 'Payment Required',
        403 => 'Forbidden',
        404 => 'Not Found',
        405 => 'Method Not Allowed',
        406 => 'Not Acceptable|',
        407 => 'Proxy Authentication Required',
        408 => 'Request Timeout',
        409 => 'Conflict',
        410 => 'Gone',
        411 => 'Length Required',
        412 => 'Precondition Failed',
        413 => 'Payload Too Large',
        414 => 'URI Too Long',
        415 => 'Unsupported Media Type',
        416 => 'Range Not Satisfiable',
        417 => 'Expectation Failed',
        418 => 'I\'m a teapot',                                               // RFC2324
        421 => 'Misdirected Request',                                         // RFC7540
        422 => '参数校验错误',                                        // RFC4918
        423 => 'Locked',                                                      // RFC4918
        424 => 'Failed Dependency',                                           // RFC4918
        425 => 'Too Early',                                                   // RFC-ietf-httpbis-replay-04
        426 => 'Upgrade Required',                                            // RFC2817
        428 => 'Precondition Required',                                       // RFC6585
        429 => 'Too Many Requests',                                           // RFC6585
        431 => 'Request Header Fields Too Large',                             // RFC6585
        451 => 'Unavailable For Legal Reasons',                               // RFC7725
        500 => 'Internal Server Error',
        501 => 'Not Implemented',
        502 => 'Bad Gateway',
        503 => 'Service Unavailable',
        504 => 'Gateway Timeout',
        505 => 'HTTP Version Not Supported',
        506 => 'Variant Also Negotiates',                                     // RFC2295
        507 => 'Insufficient Storage',                                        // RFC4918
        508 => 'Loop Detected',                                               // RFC5842
        510 => 'Not Extended',                                                // RFC2774
        511 => 'Network Authentication Required',                             // RFC6585
        520 => '服务器维护升级中，稍等片刻',
        19999 => '抱歉，系统异常！请联系龙猫管家支付处理！',
        0 => 'success',
        1000000 => 'appId不能为空',
        1000001 => '签名不能为空',
        1000002 => '非法客户端',
        1000003 => '签名验证不正确',
        1000004 => 'appKey不存在',
        1010000 => '开始时间大于结束时间',
        1020000 => '还款计划不存在',
        1030000 => '无效文件|无效文件',
        1030001 => '图片格式不支持|图片格式不支持',
        1030002 => '图片大小超出限制|图片大小超出限制',
        1030003 => '图片上传失败|图片上传失败',
        1030004 => '图片不存在|图片不存在',
        1040000 => '不存在该任务类型|不存在该任务类型',
    );

    /**
     * @var int
     */
    protected $statusCode;

    /**
     * @var string
     */
    protected $statusText;

    /**
     * @param $code
     * @param string $message
     * @param array $data
     * @param bool $defaultPrefix
     * @return \Illuminate\Http\JsonResponse
     */
    public function responseJson($code, $data = [], $defaultPrefix = 0, $message = '')
    {
        $this->statusCode = $defaultPrefix > 0 ? $defaultPrefix . $code : $code;
        $this->statusText = empty($message) ? $this->statusTexts[$this->statusCode] : $message;
        $result = [
            'code' => (int)$this->statusCode,//system返回码
            'message' => $this->statusText,//system返回码描述
        ];
        $json = empty($data) ? $result : array_merge($result, ['data' => $data]);
        return response()->json($json);
    }
}
